<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inv_sub_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function insert_sub_inv($data){
      $this->db->query("call inv_sub_ins(?,?,?,?,@id,@err,@msg)",$data);
      $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
      echo json_encode($que->result());
    }

    public function update_sub_inv($data){
      $this->db->query("call inv_sub_upd(?,?,?,?,?,@err,@msg)",$data);
      $que = $this->db->query("SELECT @err as error , @msg as msg");
      echo json_encode($que->result());
    }

    public function insert_sub_inv_locator($data){
      $this->db->query("call inv_sub_locator_ins(?,?,?,?,@id,@err,@msg)",$data);
      $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
      return $que->result();
    }

    public function update_sub_inv_locator($data){
      $this->db->query("call inv_sub_locator_upd(?,?,?,?,@err,@msg)",$data);
      $que = $this->db->query("SELECT @err as error , @msg as msg");
      return $que->result();
    }

    public function get_subInv_data($id){
      $sql = "SELECT * FROM inv_sub WHERE inv_sub_id = '".$id."' ";
       // $sql="SELECT urs.line_id line_id, urs.rate_id rate_id, uce.charge_name charge_name, urs.description description, urs.usage_qty usage_qty, urs.rate rate FROM ubs_rate_schedule urs INNER JOIN ubs_chg_elements uce ON uce.charge_code = urs.charge_code WHERE urs.rate_id = '".$id."' ";
      $query = $this->db->query($sql);
      return $query->result();

    }

    public function search_subInv($sub_code, $sub_name){
      $sql;
          if($sub_code != "" && $sub_name != ""){
            $sql = "SELECT * FROM inv_sub WHERE inv_sub_code LIKE ".$this->db->escape($sub_code)." AND inv_sub_name LIKE '".$sub_name."' ";
          }else if($sub_code == "" && $sub_name != ""){
            $sql = "SELECT * FROM inv_sub WHERE inv_sub_name LIKE ".$this->db->escape($sub_name)." ";
          }else{
            $sql = "SELECT * FROM inv_sub WHERE inv_sub_code LIKE ".$this->db->escape($sub_code)." ";
          }
          //$query = $this->db->get_where('inv_sub', array('module_code' => $modulecode, 'module_name' => $modulename), 1);
          $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }
    }


    public function get_locators($id){
        $sql = "SELECT * FROM inv_sub_locator WHERE inv_sub_id = '".$id."' ";
       // $sql="SELECT urs.line_id line_id, urs.rate_id rate_id, uce.charge_name charge_name, urs.description description, urs.usage_qty usage_qty, urs.rate rate FROM ubs_rate_schedule urs INNER JOIN ubs_chg_elements uce ON uce.charge_code = urs.charge_code WHERE urs.rate_id = '".$id."' ";
        $query = $this->db->query($sql);
        return $query->result();
    }
}